package com.py.chat.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.UUID;

import com.py.chat.bean.User;
import com.py.chat.utils.DBUtils;
import com.py.chat.utils.PropertiesUtil;

public class UserDao {

	//判断数据库中是否含有name
	public int hasName(String name){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int result = 0;
		try {
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("select count(1) as count from user where name = ?");
			ps.setString(1, name);
			rs = ps.executeQuery();
			while(rs.next()){
				result = rs.getInt("count");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	//添加用户
	public void addUser(String name,String password){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("insert into user(id,name,password,level,exp) value(?,?,?,?,?)");
			ps.setString(1,UUID.randomUUID().toString());							//id
			ps.setString(2, name);													//姓名
			ps.setString(3, password);										 		//密码
			ps.setInt(4, Integer.parseInt(PropertiesUtil.getValue("initlevel")));   //初始等级
			ps.setInt(5, Integer.parseInt(PropertiesUtil.getValue("initexp")));		//初始经验
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//判断登陆账号密码是否正确
	public User checkUser(String name,String password){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;
		try {
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("select id,name,password,level,exp,sk_num,last_send_msg_time from user where name = ? and password = ?");
			ps.setString(1, name);
			ps.setString(2, password);
			rs = ps.executeQuery();
			while(rs.next()){
				user = new User();
				user.setId(rs.getString("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setLevel(rs.getInt("level"));
				user.setExp(rs.getInt("exp"));
				user.setSkNum(rs.getInt("sk_num"));
				if(rs.getTimestamp("last_send_msg_time") != null){
					user.setLastSendMsgTime(rs.getTimestamp("last_send_msg_time").getTime());
				}
				
			}	
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return user;
	}
	//通过name获取id
	public String getId(String name){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String id = "";
		try{
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("select id from user where name = ? ");
			ps.setString(1, name);
			rs = ps.executeQuery();
			while(rs.next()){
				id = rs.getString("id");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return id;
	}
	public int getLevelById(String id){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int level = 0;
		try{
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("select level from user where id = ? ");
			ps.setString(1, id);
			rs = ps.executeQuery();
			while(rs.next()){
				level = rs.getInt("level");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return level;
	}
	public User getUserByName(String name){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = new User();
		try{
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("select * from user where name = ? ");
			ps.setString(1, name);
			rs = ps.executeQuery();
			while(rs.next()){
				String id = rs.getString("id");
				String name1 = rs.getString("name");
				int exp = rs.getInt("exp");
				int level = rs.getInt("level");
				int skNum = rs.getInt("skNum");
				user.setId(id);
				user.setName(name1);
				user.setExp(exp);
				user.setLevel(level);
				user.setExp(skNum);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}
	// 更新经验
	public void addExp(String id,int level,int exp){
		
		Connection conn = null;
		PreparedStatement ps = null;
		try{
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("update user set exp = ? , level = ? where id = ?");
			ps.setInt(1, exp);
			ps.setInt(2, level);
			ps.setString(3, id);
			ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void updateSkNumAndLastTimeById(String id,int skNum,long lastSendTime){
		
		Connection conn = null;
		PreparedStatement ps = null;
		try{
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("update user set sk_num = ? ,last_send_msg_time = ? where id = ?");
			ps.setInt(1, skNum);
			Timestamp time = new Timestamp(lastSendTime);
			
			ps.setTimestamp(2, time);
			ps.setString(3, id);
			ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				ps.close();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
